package com.bus.controller;

import com.bus.entity.Customer;
import com.bus.service.CustomerService;
import com.bus.vo.CustomerVo;
import com.common.DataGridView;
import com.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bus/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/loadCustomer")
    @ResponseBody
    public DataGridView loadCustomer(CustomerVo customerVo) {
        Map<String, Object> map = customerService.selectAllCustomer(customerVo);
        List<Customer> customers = (List<Customer>) map.get("customers");
        long total = (long) map.get("total");
        return new DataGridView(total, customers);
        /*QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        String customerName = customerVo.getCustomerName();
        String telephone = customerVo.getTelephone();
        String connectionPerson = customerVo.getConnectionPerson();
        queryWarpper.eq("available", SysConstast.AVAILABLE)
                .like(StringUtils.isNotBlank(customerName),"customerName",customerName)
                .like(StringUtils.isNotBlank(telephone),"telephone",telephone)
                .like(StringUtils.isNotBlank(connectionPerson),"connectionPerson",connectionPerson);
        IPage<Customer> page = new Page<>(customerVo.getPage(),customerVo.getLimit());
        IPage<Customer> result = customerService.page(page, queryWarpper);
        return new DataGridView(result.getTotal(),result.getRecords());*/
    }

    /**
     * 重定向到客户管理页面
     */
    @RequestMapping(value = "/customerManagement", method = RequestMethod.GET)
    public String showCustomer() {
        return "redirect:/view/data/customerManager.jsp";
    }

    @RequestMapping("/updateCustomer")
    @ResponseBody
    public ResultObj updateCustomer(CustomerVo customerVo){
        try {
            customerService.updateCustomer(customerVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }

    @RequestMapping("/addCustomer")
    @ResponseBody
    public ResultObj addCustomer(CustomerVo customerVo){
        try {
            customerService.addCustomer(customerVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }

    @RequestMapping("/deleteCustomer")
    @ResponseBody
    public ResultObj deleteCustomer(CustomerVo customerVo){
        try {
            customerService.deleteCustomer(customerVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
        return ResultObj.DELETE_SUCCESS;
    }
}
